/**
 *  Copyright 2011 Marco Berri - marcoberri@gmail.com
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and limitations under the License.
 **/
package com.mbfasttrack.service;

import com.mbfasttrack.model.Like;
import com.mbfasttrack.model.User;
import java.util.List;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Marco Berri marcoberri@gmail.com http://tecnicume.blogspot.com
 */
@Service("LikeService")
@Transactional
public class LikeService extends Base {

    /**
     * 
     * @param id
     * @return
     */
    public Long getCountByObject(String id) {

        ObjectId obj_id = new ObjectId(id);

        Query q = new Query(
                Criteria.where("obj.$id").is(obj_id).andOperator(Criteria.where("active").is(Boolean.TRUE)));



        return mongoTemplate.count(
                q,
                Like.class);

    }

    /**
     * 
     * @param username
     * @param objid
     * @return
     */
    public Boolean isUserLikeObject(String username, String objid) {

        User u = userRepository.findOneByUsername(username);

        if (u == null) {
            System.err.println(username + " is null");
            return Boolean.FALSE;
        }

        return this.isUserIdLikeObject(u.getId(), objid);

    }

    /**
     * 
     * @param userid
     * @param objid
     * @return
     */
    public Boolean isUserIdLikeObject(String userid, String objid) {

        ObjectId obj_id = new ObjectId(objid);
        ObjectId user_id = new ObjectId(userid);

        Query q = new Query(
                Criteria.where("obj.$id").is(obj_id).andOperator(Criteria.where("active").is(Boolean.TRUE).andOperator(Criteria.where("user.$id").is(user_id))));

        Long r = mongoTemplate.count(
                q,
                Like.class);

        if (r != null && r > 0) {
            return Boolean.TRUE;
        }

        return Boolean.FALSE;


    }

    /**
     * 
     * @param userid
     * @return
     */
    public List<Like> findFriendByUserId(String userid) {

        Query q = new Query(
                Criteria.where("user.$id").is(new ObjectId(userid)).andOperator(
                Criteria.where("active").is(Boolean.TRUE).where("obj.$ref").is("user")));

        System.err.println("Query: " + q.getQueryObject());

        return mongoTemplate.find(
                q,
                Like.class);


    }

    /**
     * 
     * @param obj
     * @param username
     * @param active
     * @return
     */
    public Like add(com.mbfasttrack.model.Base obj, String username, boolean active) {
        User user = userService.userRepository.findOneByUsername(username);
        return this.add(obj, user, active);

    }

    /**
     * 
     * @param obj
     * @param user
     * @param active
     * @return
     */
    public Like add(com.mbfasttrack.model.Base obj, User user, boolean active) {
        Like l = new Like();
        l.setActive(active);
        l.setObj(obj);
        l.setUser(user);
        return likeRepository.save(l);


    }
}
